package com.toannv.handler;

import java.util.List;

import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import com.toannv.card.card;
import com.toannv.definitions.KeyObjectId;
import com.toannv.definitions.ResponseId;
import com.toannv.extension.RoomExtension;
import com.toannv.gameController.CheckU;
import com.toannv.helper.Roomhelper;

public class AnBaiHandler extends BaseClientRequestHandler{

	@Override
	public void handleClientRequest(User sender, ISFSObject paras) {
		// TODO Auto-generated method stub
		int idSender = sender.getPlayerId();
		
		if(idSender == RoomExtension.UserRunning){
			//lay so diem cua quan bai va chat ma nguoi choi muon an
			final int number = paras.getInt(KeyObjectId.CARD_NUMBER);
			final int poker = paras.getInt(KeyObjectId.CARD_POKER);
			
			int idOfwinner = RoomExtension.idOfStartGame;
			
			///////////////////////////////Nguoi choi co id la 0 /////////////////////////////////
			if(idSender==0){
				
				int lastUserId = RoomExtension.NumberOfPlayers -1;//id cua nguoi danh truoc do
				
				///////////////////////////nguoi choi truoc do co id = 2
				if(lastUserId == 2){
					 card lastCardPush = RoomExtension.CardOfUser2Push.pop();
					 
					 if(lastCardPush.getNumber() == number && lastCardPush.getPoker() == poker){
						 
						//tạo đối tượng phòng chơi hiện tại
						Room CurrentRoom = Roomhelper.getCurrentRoom(this);
						//danh sách người chơi ở trong phòng
						final List<User> user = CurrentRoom.getUserList();
						
						RoomExtension.CardOfUser0Eat.push(lastCardPush);
						
						for(int i=0; i<RoomExtension.CardOfUser[idSender].length; i++){
							//neu co phan tu nao cua mang co number = 0 thi set gia tri moi
							if(RoomExtension.CardOfUser[idSender][i].getNumber() == 0){
								RoomExtension.CardOfUser[idSender][i].setNumber(lastCardPush.getNumber());
								RoomExtension.CardOfUser[idSender][i].setPoker(lastCardPush.getPoker());
								break;
							}
							//neu card la null thi tao card moi
							else if(RoomExtension.CardOfUser[idSender][i]==null){
								RoomExtension.CardOfUser[idSender][i] = new card(lastCardPush);
								break;
							}
						}
						
						new Thread(){
							public void run(){
								SFSObject sfsO = new SFSObject();
								sfsO.putInt(KeyObjectId.CARD_NUMBER, number);
								sfsO.putInt(KeyObjectId.CARD_POKER, poker);
								
								if(RoomExtension.CardOfUser0Push.size() == 3){
									send(ResponseId.AN_CHOT, sfsO, user);
								}
								else{
									send(ResponseId.AN_BAI, sfsO, user);
								}
							}
						}.start();
						
						transformCard(idOfwinner, idSender, user);
						
						RoomExtension.idOfStartGame = (idOfwinner+1)%RoomExtension.NumberOfPlayers;
						
						int check = 0;
						card Cards[] = new card[10];
						for(int i=0; i<10; i++){
							Cards[i] = new card(RoomExtension.CardOfUser[idSender][i]);
						}
						check = CheckU.checkall(idSender, Cards);
						SFSObject sfsONull = new SFSObject();
						if(check==2){
							send(ResponseId.U_THUONG, sfsONull, sender);
						}
						else if(check==3){
							send(ResponseId.U_DEN, sfsONull, sender);
						}
						else if(check==4){
							send(ResponseId.U_TRON, sfsONull, sender);
						}
						else if(check==1){
							send(ResponseId.AN_LAO, sfsONull, sender);
						}
						else if(check==5){
							send(ResponseId.U_DEN_TRON, sfsONull, sender);
						}
					 }
					 else{
						 RoomExtension.CardOfUser2Push.push(lastCardPush);
					 }
				 }
				
				///////////////////////////////nguoi choi truoc do co id = 3////////////
				else if(lastUserId == 3){
					card lastCardPush = RoomExtension.CardOfUser3Push.pop();
					 
					 if(lastCardPush.getNumber() == number && lastCardPush.getPoker() == poker){
						 
						//tạo đối tượng phòng chơi hiện tại
						Room CurrentRoom = Roomhelper.getCurrentRoom(this);
						//danh sách người chơi ở trong phòng
						final List<User> user = CurrentRoom.getUserList();
						
						RoomExtension.CardOfUser0Eat.push(lastCardPush);
						for(int i=0; i<RoomExtension.CardOfUser[idSender].length; i++){
							//neu co phan tu nao cua mang co number = 0 thi set gia tri moi
							if(RoomExtension.CardOfUser[idSender][i].getNumber() == 0){
								RoomExtension.CardOfUser[idSender][i].setNumber(lastCardPush.getNumber());
								RoomExtension.CardOfUser[idSender][i].setPoker(lastCardPush.getPoker());
								break;
							}
							//neu card la null thi tao card moi
							else if(RoomExtension.CardOfUser[idSender][i]==null){
								RoomExtension.CardOfUser[idSender][i] = new card(lastCardPush);
								break;
							}
						}
						
						new Thread(){
							public void run(){
								SFSObject sfsO = new SFSObject();
								sfsO.putInt(KeyObjectId.CARD_NUMBER, number);
								sfsO.putInt(KeyObjectId.CARD_POKER, poker);
								
								if(RoomExtension.CardOfUser0Push.size() == 3){
									send(ResponseId.AN_CHOT, sfsO, user);
								}
								else{
									send(ResponseId.AN_BAI, sfsO, user);
								}
							}
						}.start();
						
						transformCard(idOfwinner, idSender, user);
						
						RoomExtension.idOfStartGame = (idOfwinner+1)%RoomExtension.NumberOfPlayers;
						
						int check = 0;
						card Cards[] = new card[10];
						for(int i=0; i<10; i++){
							Cards[i] = new card(RoomExtension.CardOfUser[idSender][i]);
						}
						check = CheckU.checkall(idSender, Cards);
						SFSObject sfsONull = new SFSObject();
						if(check==2){
							send(ResponseId.U_THUONG, sfsONull, sender);
						}
						else if(check==3){
							send(ResponseId.U_DEN, sfsONull, sender);
						}
						else if(check==4){
							send(ResponseId.U_TRON, sfsONull, sender);
						}
						else if(check==1){
							send(ResponseId.AN_LAO, sfsONull, sender);
						}
						else if(check==5){
							send(ResponseId.U_DEN_TRON, sfsONull, sender);
						}
					 }
					 else{
						 RoomExtension.CardOfUser3Push.push(lastCardPush);
					 }
				 }
				 
				///////////////////////nguoi choi truoc do co id = 1 ////////////////////////////
				 else if(lastUserId == 1){
					 card lastCardPush = RoomExtension.CardOfUser1Push.pop();
					 
					 if(lastCardPush.getNumber() == number && lastCardPush.getPoker() == poker){
						 
						//tạo đối tượng phòng chơi hiện tại
						Room CurrentRoom = Roomhelper.getCurrentRoom(this);
						//danh sách người chơi ở trong phòng
						final List<User> user = CurrentRoom.getUserList();
						
						RoomExtension.CardOfUser0Eat.push(lastCardPush);
						for(int i=0; i<RoomExtension.CardOfUser[idSender].length; i++){
							//neu co phan tu nao cua mang co number = 0 thi set gia tri moi
							if(RoomExtension.CardOfUser[idSender][i].getNumber() == 0){
								RoomExtension.CardOfUser[idSender][i].setNumber(lastCardPush.getNumber());
								RoomExtension.CardOfUser[idSender][i].setPoker(lastCardPush.getPoker());
								break;
							}
							//neu card la null thi tao card moi
							else if(RoomExtension.CardOfUser[idSender][i]==null){
								RoomExtension.CardOfUser[idSender][i] = new card(lastCardPush);
								break;
							}
						}
						
						new Thread(){
							public void run(){
								SFSObject sfsO = new SFSObject();
								sfsO.putInt(KeyObjectId.CARD_NUMBER, number);
								sfsO.putInt(KeyObjectId.CARD_POKER, poker);
								
								if(RoomExtension.CardOfUser0Push.size() == 3){
									send(ResponseId.AN_CHOT, sfsO, user);
								}
								else{
									send(ResponseId.AN_BAI, sfsO, user);
								}
							}
						}.start();
						
						transformCard(idOfwinner, idSender, user);
						
						RoomExtension.idOfStartGame = (idOfwinner+1)%RoomExtension.NumberOfPlayers;
						
						int check = 0;
						card Cards[] = new card[10];
						for(int i=0; i<10; i++){
							Cards[i] = new card(RoomExtension.CardOfUser[idSender][i]);
						}
						check = CheckU.checkall(idSender, Cards);
						SFSObject sfsONull = new SFSObject();
						if(check==2){
							send(ResponseId.U_THUONG, sfsONull, sender);
						}
						else if(check==3){
							send(ResponseId.U_DEN, sfsONull, sender);
						}
						else if(check==4){
							send(ResponseId.U_TRON, sfsONull, sender);
						}
						else if(check==1){
							send(ResponseId.AN_LAO, sfsONull, sender);
						}
						else if(check==5){
							send(ResponseId.U_DEN_TRON, sfsONull, sender);
						}
					 }
					 else{
						 RoomExtension.CardOfUser1Push.push(lastCardPush);
					 }
				 }
			}
			
			///////////////////////////////nguoi gui co id = 1 //////////////////////////////
			else if(idSender==1){
				card lastCardPush = RoomExtension.CardOfUser0Push.pop();
				 
				 if(lastCardPush.getNumber() == number && lastCardPush.getPoker() == poker){
					 
					//tạo đối tượng phòng chơi hiện tại
					Room CurrentRoom = Roomhelper.getCurrentRoom(this);
					//danh sách người chơi ở trong phòng
					final List<User> user = CurrentRoom.getUserList();
					
					RoomExtension.CardOfUser1Eat.push(lastCardPush);
					for(int i=0; i<RoomExtension.CardOfUser[idSender].length; i++){
						//neu co phan tu nao cua mang co number = 0 thi set gia tri moi
						if(RoomExtension.CardOfUser[idSender][i].getNumber() == 0){
							RoomExtension.CardOfUser[idSender][i].setNumber(lastCardPush.getNumber());
							RoomExtension.CardOfUser[idSender][i].setPoker(lastCardPush.getPoker());
							break;
						}
						//neu card la null thi tao card moi
						else if(RoomExtension.CardOfUser[idSender][i]==null){
							RoomExtension.CardOfUser[idSender][i] = new card(lastCardPush);
							break;
						}
					}
					new Thread(){
						public void run(){
							SFSObject sfsO = new SFSObject();
							sfsO.putInt(KeyObjectId.CARD_NUMBER, number);
							sfsO.putInt(KeyObjectId.CARD_POKER, poker);
							
							if(RoomExtension.CardOfUser1Push.size() == 3){
								send(ResponseId.AN_CHOT, sfsO, user);
							}
							else{
								send(ResponseId.AN_BAI, sfsO, user);
							}
						}
					}.start();
					
					transformCard(idOfwinner, idSender, user);
					
					RoomExtension.idOfStartGame = (idOfwinner+1)%RoomExtension.NumberOfPlayers;
					
					int check = 0;
					card Cards[] = new card[10];
					for(int i=0; i<10; i++){
						Cards[i] = new card(RoomExtension.CardOfUser[idSender][i]);
					}
					check = CheckU.checkall(idSender, Cards);
					SFSObject sfsONull = new SFSObject();
					if(check==2){
						send(ResponseId.U_THUONG, sfsONull, sender);
					}
					else if(check==3){
						send(ResponseId.U_DEN, sfsONull, sender);
					}
					else if(check==4){
						send(ResponseId.U_TRON, sfsONull, sender);
					}
					else if(check==1){
						send(ResponseId.AN_LAO, sfsONull, sender);
					}
					else if(check==5){
						send(ResponseId.U_DEN_TRON, sfsONull, sender);
					}
				 }
				 else{
					 RoomExtension.CardOfUser0Push.push(lastCardPush);
				 }
			}
			
			////////////////////////////nguoi gui co id = 2 ////////////////////////////////////
			else if(idSender == 2){
				card lastCardPush = RoomExtension.CardOfUser1Push.pop();
				 
				 if(lastCardPush.getNumber() == number && lastCardPush.getPoker() == poker){
					 
					//tạo đối tượng phòng chơi hiện tại
					Room CurrentRoom = Roomhelper.getCurrentRoom(this);
					//danh sách người chơi ở trong phòng
					final List<User> user = CurrentRoom.getUserList();
					
					RoomExtension.CardOfUser2Eat.push(lastCardPush);
					for(int i=0; i<RoomExtension.CardOfUser[idSender].length; i++){
						//neu co phan tu nao cua mang co number = 0 thi set gia tri moi
						if(RoomExtension.CardOfUser[idSender][i].getNumber() == 0){
							RoomExtension.CardOfUser[idSender][i].setNumber(lastCardPush.getNumber());
							RoomExtension.CardOfUser[idSender][i].setPoker(lastCardPush.getPoker());
							break;
						}
						//neu card la null thi tao card moi
						else if(RoomExtension.CardOfUser[idSender][i]==null){
							RoomExtension.CardOfUser[idSender][i] = new card(lastCardPush);
							break;
						}
					}
					new Thread(){
						public void run(){
							SFSObject sfsO = new SFSObject();
							sfsO.putInt(KeyObjectId.CARD_NUMBER, number);
							sfsO.putInt(KeyObjectId.CARD_POKER, poker);
							
							if(RoomExtension.CardOfUser2Push.size() == 3){
								send(ResponseId.AN_CHOT, sfsO, user);
							}
							else{
								send(ResponseId.AN_BAI, sfsO, user);
							}
						}
					}.start();
					
					transformCard(idOfwinner, idSender, user);
					
					RoomExtension.idOfStartGame = (idOfwinner+1)%RoomExtension.NumberOfPlayers;
					
					int check = 0;
					card Cards[] = new card[10];
					for(int i=0; i<10; i++){
						Cards[i] = new card(RoomExtension.CardOfUser[idSender][i]);
					}
					check = CheckU.checkall(idSender, Cards);
					SFSObject sfsONull = new SFSObject();
					if(check==2){
						send(ResponseId.U_THUONG, sfsONull, sender);
					}
					else if(check==3){
						send(ResponseId.U_DEN, sfsONull, sender);
					}
					else if(check==4){
						send(ResponseId.U_TRON, sfsONull, sender);
					}
					else if(check==1){
						send(ResponseId.AN_LAO, sfsONull, sender);
					}
					else if(check==5){
						send(ResponseId.U_DEN_TRON, sfsONull, sender);
					}
				 }
				 else{
					 RoomExtension.CardOfUser1Push.push(lastCardPush);
				 }
			}
			
			///////////////////////////nguoi gui co id = 3/////////////////////////////////////////
			else if(idSender == 3){
				card lastCardPush = RoomExtension.CardOfUser2Push.pop();
				 
				 if(lastCardPush.getNumber() == number && lastCardPush.getPoker() == poker){
					 
					//tạo đối tượng phòng chơi hiện tại
					Room CurrentRoom = Roomhelper.getCurrentRoom(this);
					//danh sách người chơi ở trong phòng
					final List<User> user = CurrentRoom.getUserList();
					
					RoomExtension.CardOfUser3Eat.push(lastCardPush);
					for(int i=0; i<RoomExtension.CardOfUser[idSender].length; i++){
						//neu co phan tu nao cua mang co number = 0 thi set gia tri moi
						if(RoomExtension.CardOfUser[idSender][i].getNumber() == 0){
							RoomExtension.CardOfUser[idSender][i].setNumber(lastCardPush.getNumber());
							RoomExtension.CardOfUser[idSender][i].setPoker(lastCardPush.getPoker());
							break;
						}
						//neu card la null thi tao card moi
						else if(RoomExtension.CardOfUser[idSender][i]==null){
							RoomExtension.CardOfUser[idSender][i] = new card(lastCardPush);
							break;
						}
					}
					new Thread(){
						public void run(){
							SFSObject sfsO = new SFSObject();
							sfsO.putInt(KeyObjectId.CARD_NUMBER, number);
							sfsO.putInt(KeyObjectId.CARD_POKER, poker);
							
							if(RoomExtension.CardOfUser3Push.size() == 3){
								send(ResponseId.AN_CHOT, sfsO, user);
							}
							else{
								send(ResponseId.AN_BAI, sfsO, user);
							}
						}
					}.start();
					
					transformCard(idOfwinner, idSender, user);
					
					RoomExtension.idOfStartGame = (idOfwinner+1)%RoomExtension.NumberOfPlayers;
					
					int check = 0;
					card Cards[] = new card[10];
					for(int i=0; i<10; i++){
						Cards[i] = new card(RoomExtension.CardOfUser[idSender][i]);
					}
					check = CheckU.checkall(idSender, Cards);
					SFSObject sfsONull = new SFSObject();
					if(check==2){
						send(ResponseId.U_THUONG, sfsONull, sender);
					}
					else if(check==3){
						send(ResponseId.U_DEN, sfsONull, sender);
					}
					else if(check==4){
						send(ResponseId.U_TRON, sfsONull, sender);
					}
					else if(check==1){
						send(ResponseId.AN_LAO, sfsONull, sender);
					}
					else if(check==5){
						send(ResponseId.U_DEN_TRON, sfsONull, sender);
					}
				 }
				 else{
					 RoomExtension.CardOfUser2Push.push(lastCardPush);
				 }
			}
		}
	}
	
	private void transformCard(int idOfWinner, int idOfsender, List<User> user){
		card transformcard = null;
		if(idOfWinner==0){
			transformcard = RoomExtension.CardOfUser0Push.pop();
			if(idOfsender==1){
				RoomExtension.CardOfUser1Push.push(transformcard);
			}
			else if(idOfsender==2){
				RoomExtension.CardOfUser2Push.push(transformcard);
			}
			else if(idOfsender==3){
				RoomExtension.CardOfUser3Push.push(transformcard);
			}
		}
		else if(idOfWinner==1){
			transformcard = RoomExtension.CardOfUser1Push.pop();
			if(idOfsender==0){
				RoomExtension.CardOfUser0Push.push(transformcard);
			}
			else if(idOfsender==2){
				RoomExtension.CardOfUser2Push.push(transformcard);
			}
			else if(idOfsender==3){
				RoomExtension.CardOfUser3Push.push(transformcard);
			}
		}
		else if(idOfWinner==2){
			transformcard = RoomExtension.CardOfUser2Push.pop();
			if(idOfsender==1){
				RoomExtension.CardOfUser1Push.push(transformcard);
			}
			else if(idOfsender==0){
				RoomExtension.CardOfUser0Push.push(transformcard);
			}
			else if(idOfsender==3){
				RoomExtension.CardOfUser3Push.push(transformcard);
			}
		}
		else if(idOfWinner==3){
			transformcard = RoomExtension.CardOfUser3Push.pop();
			if(idOfsender==1){
				RoomExtension.CardOfUser1Push.push(transformcard);
			}
			else if(idOfsender==2){
				RoomExtension.CardOfUser2Push.push(transformcard);
			}
			else if(idOfsender==0){
				RoomExtension.CardOfUser0Push.push(transformcard);
			}
		}
		SFSObject sfsTranObject = new SFSObject();
		sfsTranObject.putInt(KeyObjectId.CARD_NUMBER, transformcard.getNumber());
		sfsTranObject.putInt(KeyObjectId.CARD_POKER, transformcard.getPoker());
		send(ResponseId.CHUYEN_BAI, sfsTranObject, user);
	}
	
}
